package com.zprmb.udzt.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zprmb.udzt.BaseActivity;
import com.zprmb.udzt.PayDexterityActivity;
import com.zprmb.udzt.R;
import com.zprmb.udzt.V6LoginActivity;
import com.zprmb.udzt.V6WebviewChild;
import com.zprmb.udzt.bean.JiaoYanFailBean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.ExplosionField;
import com.zprmb.udzt.util.LogUtils;
import com.zprmb.udzt.util.MySharePreferenceUtil;
import com.zprmb.udzt.util.NewVolleryUtil;
import com.zprmb.udzt.util.NumUtil;
import com.zprmb.udzt.util.Regular;
import com.zprmb.udzt.util.TimeUtils;
import com.zprmb.udzt.util.ToastUtil;
import com.zprmb.udzt.util.VolleryUtil;
import com.zprmb.udzt.zhitoudialog.ZhiTouAlertDialog;

import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.zprmb.udzt.R.id.tv_availableAmount;


public class V6NewZhiTouDexterityPayInActivity extends BaseActivity implements OnClickListener, ZhiTouAlertDialog.OnSureClickListener {
    //    private AddAndSubView addAndSubView;
    private TextView tv_v6_dexterity_pay_in_keyong;
    //    private TextView tv_v6_dexterity_pay_in_chiyou;
    private TextView tv_v6_dexterity_pay_in_shouyi;
    private Button tv_v6_dexterity_pay_in_updata;
    private Double million;
    private Double inAmount;
    private TextView tv_product_shouyi;
    private int remainAmount;
    private int increaseNum;// 增量
    private int minAmount;

    private Double maxAmount;


    private Double availableAmount;
    private ListView lv_comment;

    private TextView check_mAgreement;
    private CheckBox check;
    private LinearLayout li_dexterity;
    private ExplosionField mExplosionField;//粉碎动画执行方法类
    private TextView tv_v6_dexterity_pay_in_daotime;
    private TextView tv_v6_dexterity_pay_in_shengyu;
    DecimalFormat df = new DecimalFormat("###0.00");
    private List<String> list = new ArrayList<String>();
    //
    private TextView tv_mAgreement;

    String receivemoney = "";

    //输入框的money

    private EditText edit_zhitoumoney;
//剩余可投金额

    String shengyu;

    //剩余可投金额的布局

    LinearLayout layout_investall;

    TextView text_yue;

    TextView text_quantou;

    //清理了
    ImageView iv_cleardadta;
    LinearLayout layout_clear;

    //整体布局

    LinearLayout li_all;

    RelativeLayout rl_back;

    ImageView img_back;
    TextView tv_guize;

    TextView tv_title;


    private final String ACTION_NAME = "智投宝结束";
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
//                Toast.makeText(V6DetailsActivity.this, "处理action名字相对应的广播", 200);
                finish();
            }
        }

    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        //注册广播
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_v6newzhitou);
        setImmersionStatus();
        setContentInview(this).setVisibility(View.INVISIBLE);
        rl_back = (RelativeLayout) findViewById(R.id.rl_back);
        rl_back.setOnClickListener(this);
        //标题
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("转入");
        img_back = (ImageView) findViewById(R.id.img_back);
        img_back.setOnClickListener(this);
        tv_guize = (TextView) findViewById(R.id.tv_guize);
        tv_guize.setOnClickListener(this);
        setgoback(V6NewZhiTouDexterityPayInActivity.this);
        check = (CheckBox) findViewById(R.id.check);
        registerBoradcastReceiver();
        tv_mAgreement = (TextView) findViewById(R.id.tv_mAgreement);
        check_mAgreement = (TextView) findViewById(R.id.check_mAgreement);
        li_dexterity = (LinearLayout) findViewById(R.id.li_dexterity);
        layout_clear = (LinearLayout) findViewById(R.id.layout_clear);
        iv_cleardadta = (ImageView) findViewById(R.id.iv_cleardadta);
        tv_v6_dexterity_pay_in_shengyu = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_shengyu);
//        addAndSubView = (AddAndSubView) findViewById(R.id.add_subview);
        tv_v6_dexterity_pay_in_keyong = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_keyong);
//        tv_v6_dexterity_pay_in_chiyou = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_chiyou);
        tv_v6_dexterity_pay_in_shengyu = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_shengyu);
        tv_v6_dexterity_pay_in_updata = (Button) findViewById(R.id.tv_v6_dexterity_pay_in_updata);
        tv_v6_dexterity_pay_in_daotime = (TextView) findViewById(R.id.tv_v6_dexterity_pay_in_daotime);
        tv_product_shouyi = (TextView) findViewById(R.id.tv_product_shouyi);

        receivemoney = String.valueOf(getIntent().getIntExtra("investamount", 0));
        edit_zhitoumoney = (EditText) findViewById(R.id.edit_zhitoumoney);
        edit_zhitoumoney.setText(receivemoney);
        if (edit_zhitoumoney.equals("0")) {
            tv_v6_dexterity_pay_in_updata.setEnabled(false);
            tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.btn_unclick);
        }

        tv_product_shouyi.setVisibility(View.INVISIBLE);
//投资按钮
        tv_v6_dexterity_pay_in_updata.setOnClickListener(this);
//整体布局
        li_all = (LinearLayout) findViewById(R.id.li_all);
        edit_zhitoumoney.requestFocus();

        layout_investall = (LinearLayout) findViewById(R.id.layout_investall);
        text_yue = (TextView) findViewById(R.id.text_yue);
        text_quantou = (TextView) findViewById(R.id.text_quantou);
        layout_investall.setOnClickListener(this);
        text_yue.setOnClickListener(this);
        text_quantou.setOnClickListener(this);
        check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if (isChecked) {
                    tv_v6_dexterity_pay_in_updata.setEnabled(true);
                    tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.v6button);
                } else {
                    tv_v6_dexterity_pay_in_updata.setEnabled(false);
                    tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.btn_unclick);
                }
            }
        });
        check_mAgreement.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (check.isChecked()) {
                    tv_v6_dexterity_pay_in_updata.setEnabled(false);
                    tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.btn_unclick);
                    check.setChecked(false);
                } else {
                    tv_v6_dexterity_pay_in_updata.setEnabled(true);
                    tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.v6button);
                    check.setChecked(true);
                }
            }
        });
        tv_mAgreement.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent2 = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                        WebZhitouActivity.class);
                intent2.putExtra("url", "landing/currentAgreement?app=true&token=");

                intent2.putExtra("share", 1);
                intent2.putExtra("titleview", 1);
                intent2.putExtra("title", "《智投宝授权委托书》");
                startActivity(intent2);
            }
        });
        if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() <= 0) {
//            btn_invest.setBackground(getResources().getDrawable(R.drawable.v6_ud_bg_gray));

            tv_v6_dexterity_pay_in_updata.setEnabled(false);
            tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.v6greybutton);
        }
//        verityUser();
        edit_zhitoumoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (edit_zhitoumoney.getText().length() != 0) {
                    iv_cleardadta.setVisibility(View.VISIBLE);
                    tv_v6_dexterity_pay_in_updata.setEnabled(true);
                    tv_v6_dexterity_pay_in_updata.setBackgroundResource(R.drawable.v6button);
                    tv_v6_dexterity_pay_in_updata.setTextColor(getResources().getColor(R.color.white));
                    int b = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue();
                } else {
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
            }
        });
        /**焦点变化监听**/
        edit_zhitoumoney.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View arg0, boolean arg1) {
                // TODO Auto-generated method stub
                if (edit_zhitoumoney.getText().length() != 0) {
                    //删除图标显示
                    iv_cleardadta.setVisibility(View.VISIBLE);
                } else {
                    //删除图标隐藏
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
                if (arg1) {
                    //得到焦点
                } else {
                    //失去焦点，删除图标隐藏
                    iv_cleardadta.setVisibility(View.INVISIBLE);
                }
            }
        });
        //删除图标的点击监听事件
        iv_cleardadta.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // 执行清空EditText数据
                edit_zhitoumoney.setText("");
            }
        });
        layout_clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                edit_zhitoumoney.setText("");
            }
        });
        payIn();
        /*if (MySharePreferenceUtil.getBoolean(V6NewZhiTouDexterityPayInActivity.this, SharePreferenceUtil.FIRST_OPEN, false) == false) {
            li_all.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showPopupWindow2(li_all);
                }
            }, 500);
        } else {
            MySharePreferenceUtil.putBoolean(V6NewZhiTouDexterityPayInActivity.this, SharePreferenceUtil.FIRST_OPEN, true);
        }*/

        // 如果是第一次启动，则先进入功能引导页
        if (MySharePreferenceUtil.get(V6NewZhiTouDexterityPayInActivity.this, "zhitou", "").equals("1")) {
            li_all.postDelayed(new Runnable() {
                @Override
                public void run() {
                    showPopupWindow2(li_all);
                }
            }, 500);

        }
        /*li_all.postDelayed(new Runnable() {
            @Override
            public void run() {
                showPopupWindow2(li_all);
            }
        }, 500);*/
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        if (hasFocus) {
//           li_all.performClick();
        }
    }

    private PopupWindow popWindow2;

    private void showPopupWindow2(View parent) {
        if (popWindow2 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_second, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow2 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop2(view2);
        }
        //设置动画效果
        popWindow2.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow2.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow2.setOutsideTouchable(true);
        popWindow2.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow2.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow2.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop2(View view) {
        final LinearLayout layout_pop_first = (LinearLayout) view.findViewById(R.id.layout_pop_first);

        layout_pop_first.getBackground().setAlpha(175);
        layout_pop_first.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow2.dismiss();
                showPopupWindow3(layout_pop_first);
            }
        });
    }

    PopupWindow popWindow4;

    PopupWindow popWindow3;

    private void showPopupWindow3(View parent) {
        if (popWindow3 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_third, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow3 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop3(view2);
        }
        //设置动画效果
        popWindow3.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow3.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow3.setOutsideTouchable(true);
        popWindow3.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow3.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow3.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop3(View view) {
        final LinearLayout layout_pop_third = (LinearLayout) view.findViewById(R.id.layout_pop_third);

        layout_pop_third.getBackground().setAlpha(175);
        layout_pop_third.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow3.dismiss();
//                showPopupWindow4(layout_pop_third);
                showPopupWindow4(layout_pop_third);
            }
        });

        Button tv_v6_dexterity_pay_in_updata = (Button) view.findViewById(R.id.tv_v6_dexterity_pay_in_updata);
        tv_v6_dexterity_pay_in_updata.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow3.dismiss();
//                showPopupWindow4(layout_pop_third);
                showPopupWindow4(layout_pop_third);
            }
        });
    }


    private void showPopupWindow4(View parent) {
        if (popWindow4 == null) {
            View view2 = LayoutInflater.from(this)
                    .inflate(R.layout.pop_fourth, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow4 = new PopupWindow(view2, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop4(view2);
        }
        //设置动画效果
        popWindow4.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow4.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow4.setOutsideTouchable(true);
        popWindow4.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow4.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow4.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void initPop4(View view) {
        LinearLayout layout_pop_third = (LinearLayout) view.findViewById(R.id.layout_pop_fourth);

        layout_pop_third.getBackground().setAlpha(175);
        layout_pop_third.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow4.dismiss();
//                MySharePreferenceUtil.getBoolean(V6NewZhiTouDexterityPayInActivity.this, SharePreferenceUtil.FIRST_OPEN1, true);
                MySharePreferenceUtil.put(V6NewZhiTouDexterityPayInActivity.this, "zhitou", "2");
            }
        });

        Button button = (Button) view.findViewById(R.id.tv_v6_dexterity_pay_in_updata);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow4.dismiss();
//                MySharePreferenceUtil.getBoolean(V6NewZhiTouDexterityPayInActivity.this, SharePreferenceUtil.FIRST_OPEN1, true);
                MySharePreferenceUtil.put(V6NewZhiTouDexterityPayInActivity.this, "zhitou", "2");
            }
        });
    }

    String s = "";

    private void payIn() {
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                LogUtils.e("转入", jsonObject.toString());

                if (jsonObject.optInt("success") == 1) {
                    setContentInview(V6NewZhiTouDexterityPayInActivity.this).setVisibility(View.VISIBLE);

                    JSONObject json = jsonObject.optJSONObject("data");
                    tv_v6_dexterity_pay_in_keyong.setText(df.format(Double
                            .parseDouble(json.optString("userFundAvailableAmount")))
                    );
                  /*  tv_v6_dexterity_pay_in_chiyou.setText(df.format(Double
                            .parseDouble(json.optString("currentAmount")))
                    );*/

                    remainAmount = json.optInt("maxAmount");
                    increaseNum = json.optInt("increaseNum");
                    minAmount = json.optInt("minAmount");
                    maxAmount = json.optDouble("holdCapAmount");
                    tv_v6_dexterity_pay_in_shengyu.setText(NumUtil.getQiantwo(json.optString("maxAmount")) + "元");
                    shengyu = json.optString("maxAmount");

                    String time = TimeUtils.formatTime(new Date(Long
                            .valueOf(json.optString("nextWorkDay"))));
                    Log.e("时间", time);

                    tv_v6_dexterity_pay_in_daotime.setText("预期收益到账时间 :" + time);
                    productId = json.optString("productId");
                    inAmount = json.optDouble("maxTransferAmount");
                    availableAmount = json.optDouble("availableAmount");

//					addAndSubView.setMinnum(minAmount);
                    Gson gson = new Gson();
                    list = gson.fromJson(json.optString("promptList"),
                            new TypeToken<ArrayList<String>>() {
                            }.getType());
//

                    for (int i = 0; i < list.size(); i++) {
                        s = s + list.get(i);
                    }




                   /* addAndSubView.setincreaseNum(increaseNum);
                    if (getIntent().getIntExtra("investamount", 0) != 0) {
                        addAndSubView.setNum(getIntent().getIntExtra("investamount", 0));
                    }*/
                } else {
                    if (jsonObject.optString("comment").equals("please login")) {
                        Intent intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                V6LoginActivity.class);
                        intent.putExtra("close", 1);
                        intent.putExtra("web", 1);
                        startActivity(intent);
                    } else {
                        ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                                jsonObject.optString("comment"));
                    }

                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub
                setContentInview(V6NewZhiTouDexterityPayInActivity.this).setVisibility(View.INVISIBLE);

                Toast.makeText(V6NewZhiTouDexterityPayInActivity.this, "请求失败   请检查网络设置",
                        2000).show();
            }
        };
        vollery.setShow(true);
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setRequestMethod(1);
        vollery.execute(this, Constant.URL + "/api/v2/claim/fund/intelli/in", "");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        li_all.performClick();

//        payIn();
    }

   /* private void changeComment() {
        if (addAndSubView.getNum() > availableAmount) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"请输入转入金额");

//            tv_tishi.setText("账户可用余额不足");
            return;
        }
        if (addAndSubView.getNum() <= 0) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"请输入转入金额");

//            tv_tishi.setText("请输入转入金额");
            return;
        }
        if (addAndSubView.getNum() < minAmount) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"转入金额" + minAmount + "元起");

//            tv_tishi.setText("转入金额" + minAmount + "元起");
            return;
        }

        if (addAndSubView.getNum() > inAmount
                && addAndSubView.getNum() <= remainAmount) {


//            tv_tishi.setText("转入金额超出持有上限");
            return;
        }
        if (addAndSubView.getNum() > maxAmount
                ) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"转入金额超出持有上限");

//            tv_tishi.setText("输入金额超出持有上限" + Regular.getDecimalFormatZero(maxAmount / 10000) + "万元");
            return;
        }
        if (addAndSubView.getNum() > remainAmount) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"项目剩余金额不足");

//            tv_tishi.setText("项目剩余金额不足");
            return;
        }
       c
        if ((addAndSubView.getNum()
                - minAmount) % increaseNum != 0 || addAndSubView.getNum() > remainAmount || addAndSubView.getNum() > inAmount
                && addAndSubView.getNum() < remainAmount || addAndSubView.getNum() < minAmount || addAndSubView.getNum() <= 0) {

        } else {

        }
    }*/

    private void verityUser() {
        VolleryUtil vollery = new VolleryUtil() {

            @Override
            public void onUpDate(JSONObject jsonObject) {
                // TODO Auto-generated method stub
                if (jsonObject.optInt("success") == 1) {
                    Log.d(jsonObject.toString(), "44444444444444");
                    Gson gson = new Gson();
                  /*  ZhiTouBean zhiTouBean = new ZhiTouBean();
                    zhiTouBean = gson.fromJson(jsonObject.toString(), ZhiTouBean.class);*/
                    JSONObject json = jsonObject.optJSONObject("data");
                    Intent intent = null;

                    if (!json.optBoolean("isBindCard")) {
                        intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                V6WebviewChild.class);
                        intent.putExtra("url", Constant.URL + "h5/account/bankcard/bind/view?app=true&token=");
                        intent.putExtra("title", "智投宝");
                        startActivity(intent);
                        return;
                    }

                   /* intent = new Intent(V6ZhiTouDexterityPayInActivity.this,
                            PayZhitouActivity.class);*/


                    if (Double.valueOf(tv_v6_dexterity_pay_in_keyong.getText().toString()).intValue() > Double.valueOf(edit_zhitoumoney.getText().toString()).intValue()) {
                        intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                PayDexterityActivity.class);
                   /*     intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                PayZhitouActivity.class);*/

                        intent.putExtra("type", "IN");


                        tv_v6_dexterity_pay_in_updata
                                .setBackgroundResource(R.drawable.btn_unclick);
                        tv_v6_dexterity_pay_in_updata.setEnabled(false);
                        tv_v6_dexterity_pay_in_updata
                                .setBackgroundResource(R.drawable.v6button);
                        tv_v6_dexterity_pay_in_updata.setEnabled(true);
//                    intent.putExtra("amount", addAndSubView.getNum() + "");
                        intent.putExtra("amount", edit_zhitoumoney.getText().toString());

                        intent.putExtra("productId", productId);
                        LogUtils.e("智投宝id", productId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_in_from_bottom,
                                R.anim.slide_out_to_bottom);
                    } else {
                        intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                PayZhitouActivity.class);

                        intent.putExtra("amount", "100");
                        intent.putExtra("path", "100");
                /*        intent.putExtra("card_no", zhiTouBean.getData().getCardNo());
                        intent.putExtra("bank_name", zhiTouBean.getData().getBankNameEn());*/
                        intent.putExtra("loan_id", productId);
                        intent.putExtra("type", "IN");
                        intent.putExtra("investment_Amount", "400");
                        intent.putExtra("coupon_id", "");
                        intent.putExtra("coupon_type", "");
                      /*  hpay/v3/h5recharge
                        String amount 充值金额
                        String path 充值类型
                        String card_no 银行卡号
                        bank_name 银行编号
                        String token
                        String loan_id 产品ID
                        String type 产品类型
                        String investment_Amount 投资金额
                        String coupon_id 道具id
                        String coupon_type 道具类型*/


                        tv_v6_dexterity_pay_in_updata
                                .setBackgroundResource(R.drawable.btn_unclick);
                        tv_v6_dexterity_pay_in_updata.setEnabled(false);
                        tv_v6_dexterity_pay_in_updata
                                .setBackgroundResource(R.drawable.v6button);
                        tv_v6_dexterity_pay_in_updata.setEnabled(true);
//                    intent.putExtra("amount", addAndSubView.getNum() + "");
                        intent.putExtra("amount", edit_zhitoumoney.getText().toString());

                        intent.putExtra("productId", productId);
                        LogUtils.e("智投宝id", productId);
                        startActivity(intent);
                        overridePendingTransition(R.anim.slide_in_from_bottom,
                                R.anim.slide_out_to_bottom);
                    }

                } else {
                    if (jsonObject.optString("comment").equals("please login")) {
                        Intent intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                                V6LoginActivity.class);
                        startActivity(intent);
                    } else {
                        ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                                jsonObject.optString("comment"));
                    }
                }
            }

            @Override
            public void onError() {
                // TODO Auto-generated method stub

            }
        };
        vollery.setHeaders("token", getToken());
        vollery.setHeaders("client", "android");
        vollery.setShow(true);
        vollery.setRequestMethod(0);
        vollery.execute(V6NewZhiTouDexterityPayInActivity.this, Constant.URL
                + "/api/v5/user/userInfo", "");
    }

    /*  hpay/v3/h5recharge
      String amount 充值金额
      String path 充值类型
      String card_no 银行卡号
      bank_name 银行编号
      String token
      String loan_id 产品ID
      String type 产品类型
      String investment_Amount 投资金额
      String coupon_id 道具id
      paramMap.put("client", Source.WEB.name

  ());
      String coupon_type 道具类型*/
    private void setImmersionStatus() {
        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            // 透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            // 透明导航栏
            //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }


    String path;
    String myamount;
    String bank_name;
    String investment_Amount;
    String bankName;
    String card_no;
    String type2;
    String loan_id;
    String productId;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_v6_dexterity_pay_in_updata:
                if (check.isChecked() == false) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "请阅读并同意智投宝授权委托书");
                } /*else if (Double.valueOf(shengyu).intValue() <= 0) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "当前剩余可投金额为0元不可转入");
                }*/ /*else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() <= 0) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "转入金额不能为0");
                }*/
                if ((Double.valueOf(edit_zhitoumoney.getText().toString()).intValue()
                        - minAmount) % increaseNum != 0) {
//			ToastUtil.showCenterToast(V6DexterityPayInActivity.this,
//					"转入金额的增量要为" + increaseNum + "的倍数"); else if (TextUtils.isEmpty(edit_zhitoumoney.getText().toString())) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "转入金额的增量要为" + increaseNum + "的倍数");

                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() < minAmount) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "投资金额不能小于最低起投金额");


                } else if (Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() > maxAmount) {
                    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                            "投资金额不能大于" + maxAmount.intValue() + "!");
//                    return;
                } else {
                    AjaxParams params = new AjaxParams();
                    params.put("productId", "");
                    //金额传实际支付的

//                    params.put("type", "ASSET_COMMON");
                    params.put("amount", edit_zhitoumoney.getText().toString());
//                    params.put("useCurrentAmountToInvest", "true");
              /*      params.put("type", "ASSET_COMMON");
                    params.put("useCurrentAmountToInvest", "true");*/

                    VolleryUtil volleryUtil = new VolleryUtil() {
                        @Override
                        public void onUpDate(JSONObject jsonObject) {
                            // TODO Auto-generated method stub
                            if (jsonObject.optInt("success") == 1) {
                                LogUtils.e("加息券校验", jsonObject.toString());
                                Log.d(jsonObject.toString(), "444444444444");
                                showPopupWindow(tv_v6_dexterity_pay_in_updata);

                            } else {
                                JiaoYanFailBean jiaoYanFailBean = new JiaoYanFailBean();
                                Gson gson = new Gson();
                                try {
                                    jiaoYanFailBean = gson.fromJson(jsonObject.toString(), JiaoYanFailBean.class);
                                /*    ToastUtil.showCenterToast(V6NewZhiTouDexterityPayInActivity.this,
                                            jsonObject.optString("comment") + "!");*/
                                    path = jiaoYanFailBean.getData().getPath();
                                    myamount = jiaoYanFailBean.getData().getAmount();
                                    bank_name = jiaoYanFailBean.getData().getBankName();
                                    card_no = jiaoYanFailBean.getData().getCard_no();
                                    type2 = jiaoYanFailBean.getData().getType();
                                    loan_id = jiaoYanFailBean.getData().getLoan_id();
                                    productId = jiaoYanFailBean.getData().getProductId();
                                    getBank_name = jiaoYanFailBean.getData().getBank_name();
                                    showPopupWindow(tv_v6_dexterity_pay_in_updata);
                                } catch (Exception e) {
                                }


                            }
                        }

                        @Override
                        public void onError() {
                            // TODO Auto-generated method stub

                        }
                    };
                    volleryUtil.setHeaders("token", getToken());
                    volleryUtil.setHeaders("deviceId", getdeviceId());
                    volleryUtil.setRequestMethod(1);
                    volleryUtil.setShow(true);
//                    volleryUtil.execute(V6NewZhiTouDexterityPayInActivity.this, Constant.URL + "/api/v6/invest/invests/valid?", params.toString());
                    volleryUtil.execute(V6NewZhiTouDexterityPayInActivity.this, Constant.URL + "/api/v3/claim/fund/invests/valid?", params.toString());
//                    http://test1.udrmb.com/api/v3/claim/fund/invests/valid


                }
//           /     verityUser();
//                getBorrowuse();


                break;
            case R.id.layout_investall:
            case R.id.text_yue:

            case R.id.text_quantou:

                Log.d(tv_v6_dexterity_pay_in_keyong.getText().toString(), "111111111111111111");
                Log.d(minAmount + "", "111111111111111111");
                Log.d(increaseNum + "", "111111111111111111");

                int a = Double.valueOf(tv_v6_dexterity_pay_in_keyong.getText().toString()).intValue() - minAmount;
                double b = (double) a / increaseNum;

                Double f = Math.floor(b);
                int c = f.intValue();
//                int c = Double.valueOf(b).intValue();
//                    edit_zhitoumoney.setText(String.valueOf(c));
//                    Toast.makeText(this, a + "", Toast.LENGTH_SHORT).show();

                int d = c * increaseNum + minAmount;
                edit_zhitoumoney.setText(String.valueOf(d));
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());
                edit_zhitoumoney.setText(d + "");
                edit_zhitoumoney.setSelection(edit_zhitoumoney.length());

               /* String keyongmoney = tv_v6_dexterity_pay_in_keyong.getText().toString();
                int a = Double.valueOf(keyongmoney).intValue();

                if (Double.valueOf(keyongmoney).intValue() < 100) {
                    Toast.makeText(this, "投资金额不能低于100元", Toast.LENGTH_SHORT).show();
                } else if (a%100!=0) {
                    Toast.makeText(this, "投资金额为100的整数倍", Toast.LENGTH_SHORT).show();

                }else if (){

                }*/
//                edit_zhitoumoney.setText(String.valueOf(b));


                break;
         /*   RelativeLayout rl_back;

            ImageView img_back;
            TextView tv_guize;*/

            case R.id.rl_back:
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_guize:

                Intent intent = new Intent(V6NewZhiTouDexterityPayInActivity.this, RuleWebActivity.class);
//                intent.putExtra("url", Constant.URL + "api/v6/invest/ztbRules");
                intent.putExtra("content", s);
                intent.putExtra("title", "规则说明");
//                intent.putExtra("content", s);
                startActivity(intent);


                break;


        }


    }

    private PopupWindow popWindow;

    private void showPopupWindow(View parent) {
        if (popWindow == null) {
            View view = LayoutInflater.from(this)
                    .inflate(R.layout.pop_select_photo2, null);
            //LayoutParams相当于一个Layout的信息包，它封装了Layout的位置、高、宽等信息。
            popWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            initPop(view);
        }
        //设置动画效果
        popWindow.setAnimationStyle(android.R.style.Animation_InputMethod);
        //获取popwindow焦点
        popWindow.setFocusable(true);
        //设置popwindow如果点击外面区域，便关闭。
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        //实现软键盘不自动弹出,ADJUST_RESIZE属性表示Activity的主窗口总是会被调整大小，从而保证软键盘显示空间。
        popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //设置popwindow显示位置
        popWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    String getBank_name;
    Double string;

    public void initPop(View view) {
        //头部
        View pop_top = view.
                findViewById(R.id.pop_top);
        pop_top.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                popWindow = null;
            }
        });
//箭头
        ImageView iv_v4_pay_finish = (ImageView) view.findViewById(R.id.iv_v4_pay_finish);
        iv_v4_pay_finish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                popWindow = null;
            }
        });

        //投资金额

        TextView text_investmoney = (TextView) view.findViewById(R.id.text_investmoney);

//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())) + "元");
        String resule = edit_zhitoumoney.getText().toString();
        DecimalFormat df = new DecimalFormat("#,###");
        String m = df.format(Double.valueOf(resule).intValue());

        //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
        text_investmoney.setText(m + ".00" + "元");

//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");

        //带银行卡的
        LinearLayout layout_yinhangpay = (LinearLayout) view.findViewById(R.id.layout_yinhangpay);

        TextView text_yinhang = (TextView) view.findViewById(R.id.text_yinhang);

        //充值投资的按钮
        TextView text_bangname = (TextView) view.findViewById(R.id.text_bangname);

        //银行布局

        LinearLayout layout_yinhang = (LinearLayout) view.findViewById(R.id.layout_yinhang);

        Button btn_dexterity_invest = (Button) view.findViewById(R.id.btn_dexterity_invest);
        text_bangname.setText(getBank_name);

        iv_v4_pay_finish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
                popWindow = null;
            }
        });
        //余额
        TextView text_useyue = (TextView) view.findViewById(R.id.text_useyue);
        TextView bank_wei = (TextView) view.findViewById(R.id.bank_wei);

        //输入的投资
        final BigDecimal b1 = new BigDecimal(edit_zhitoumoney.getText().toString());
        //可用
        final BigDecimal b2 = new BigDecimal(tv_v6_dexterity_pay_in_keyong.getText().toString());
        View layout_kongbai = view.findViewById(R.id.layout_kongbai);
        //带银行卡的

        //余额小于投资金额
        if (b1.compareTo(b2) > 0) {
            int a = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Double.valueOf(tv_v6_dexterity_pay_in_keyong.getText().toString()).intValue() % 100;
            int n = 4;
            bank_wei.setText("(尾号:" + card_no.substring(card_no.length() - 4, card_no.length()) + ")");
//            text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(String.valueOf(a))) + "元");
            text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(tv_v6_dexterity_pay_in_keyong.getText().toString())) + "元");
            //余额小于于投资金额
//            int c = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - Double.valueOf(tv_availableAmount.getText().toString()).intValue();
//            myamout = Double.valueOf(edit_zhitoumoney.getText().toString()).intValue() - a;
            BigDecimal b3 = new BigDecimal(edit_zhitoumoney.getText().toString());
            BigDecimal b4 = new BigDecimal(tv_v6_dexterity_pay_in_keyong.getText().toString());
            string = b3.subtract(b4).doubleValue();
            string = b3.subtract(b4).doubleValue();

            DecimalFormat df2 = new DecimalFormat("#,###");
            String m1 = df2.format(Double.valueOf(string).intValue());

//            text_yinhang.setText(m1 + ".00" + "元");


            text_yinhang.setText(Regular.getDecimalFormatTwo(Double.valueOf(string)) + "元");
         /*   DecimalFormat df = new DecimalFormat("#,###");
            String m = df.format(Double.valueOf(resule).intValue());

            //        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())).toString() + "元");
//        text_investmoney.setText(Regular.getDecimalFormatTwo(Double.valueOf(m)).toString() + "元");
            text_investmoney.setText(m + ".00" + "元");
*/
            layout_yinhangpay.setVisibility(View.VISIBLE);
            layout_kongbai.setVisibility(View.GONE);
            layout_yinhang.setVisibility(View.VISIBLE);
            btn_dexterity_invest.setText("充值并投资");


        } else {
         /*   text_useyue.setText(edit_zhitoumoney.getText().toString() + "元");
            //余额大于投资金额
            text_useyue.setText(Regular.getDecimalFormatTwo(Double.valueOf(edit_zhitoumoney.getText().toString())) + "元");*/
            DecimalFormat df2 = new DecimalFormat("#,###");
            String m1 = df2.format(Double.valueOf(Double.valueOf(edit_zhitoumoney.getText().toString())).intValue());

            text_useyue.setText(m1 + ".00" + "元");


            btn_dexterity_invest.setText("余额支付");
//            layout_yinhang.setVisibility(View.GONE);
            layout_kongbai.setVisibility(View.VISIBLE);
            layout_yinhangpay.setVisibility(View.GONE);
            layout_yinhang.setVisibility(View.GONE);
        }

        btn_dexterity_invest.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
//                verityUser();
                Intent intent;
                String newtoken = getToken();
                try {
                    newtoken = URLEncoder.encode(newtoken, "UTF-8");
//                        myurl1= URLEncoder.encode(myurl1, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                }
                if (b1.compareTo(b2) > 0) {
                    AjaxParams params = new AjaxParams();
                    params.put("amount", string + "");//1
                    params.put("path", path);//1
//                    params.put("card_no", card_no);//1
                    params.put("cardNo", card_no);//1
//                    params.put("investment_Amount", investment_Amount);//1
                    params.put("investment_Amount", edit_zhitoumoney.getText().toString());//1
                    params.put("loan_id", loan_id);//1
                    params.put("client", "android");
                    params.put("token", newtoken);//1
//                    params.put("type", type2);
                    params.put("type", "CURRENT_DEPOSIT");
//                    params.put("bank_name", bank_name);//1
                    NewVolleryUtil newVolleryUtil = new NewVolleryUtil() {
                        @Override
                        public void onError() {

                        }

                        @Override
                        public void onUpDate(String jsonObject) {
//                            Log.d(jsonObject,"4444444444444444");
                            Intent intent = new Intent(V6NewZhiTouDexterityPayInActivity.this, NewV6WebviewChild2.class);
//                            intent.putExtra("url", url);
                            intent.putExtra("url", jsonObject.toString());

                            startActivity(intent);
                            popWindow.dismiss();
                            popWindow = null;

                        }

                    };
//                    vollery.OnButtonListener(V6iNewInvestCoupActivity.this);
                    newVolleryUtil.setShow(true);
                    newVolleryUtil.setRequestMethod(1);
                    newVolleryUtil.setHeaders("token", getToken());
                    newVolleryUtil.setHeaders("deviceId", getdeviceId());
                    newVolleryUtil.setHeaders("client", "android");
                    newVolleryUtil.execute(V6NewZhiTouDexterityPayInActivity.this,
                            Constant.URL + "hpay/v3/h5recharge?",
                            params.toString());
                } else {
                    intent = new Intent(V6NewZhiTouDexterityPayInActivity.this,
                            PayDexterityActivity.class);
                    intent.putExtra("type", "IN");

//                    CURRENT_DEPOSIT
                    tv_v6_dexterity_pay_in_updata
                            .setBackgroundResource(R.drawable.btn_unclick);
                    tv_v6_dexterity_pay_in_updata.setEnabled(false);
                    tv_v6_dexterity_pay_in_updata
                            .setBackgroundResource(R.drawable.v6button);
                    tv_v6_dexterity_pay_in_updata.setEnabled(true);
//                    intent.putExtra("amount", addAndSubView.getNum() + "");
                    intent.putExtra("amount", edit_zhitoumoney.getText().toString());

                    intent.putExtra("productId", productId);
                    LogUtils.e("智投宝id", productId);
                    startActivity(intent);
                    popWindow.dismiss();
                    popWindow = null;
                    overridePendingTransition(R.anim.slide_in_from_bottom,
                            R.anim.slide_out_to_bottom);
                }


            }
        });

    }


    @Override
    public void onConfirm() {

    }

    @Override
    public void onCancle() {

    }


}
